﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace YouTubeStormer
{
    public partial class Log : UserControl
    {
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern int SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam);
        private const int WM_VSCROLL = 0x115;
        private const int SB_BOTTOM = 7;

        Timer logUpdateTimer = new Timer();
        string logText = "";

        List<Log.LogOject> logObjects = new List<LogOject>();

        public Log()
        {
            InitializeComponent();
            logUpdateTimer.Interval = 800;
            logUpdateTimer.Tick += logUpdateTimer_Tick;
            logUpdateTimer.Start();
        }

        void logUpdateTimer_Tick(object sender, EventArgs e)
        {
            if (logObjects.Count > 0)
            {
                int selectionStart = richTextBoxWithLinking.SelectionStart;
                int selectionLength = richTextBoxWithLinking.SelectionLength;

                var newlogObjects = logObjects.ToArray();
                logObjects.Clear();
                foreach (Log.LogOject logObject in newlogObjects)
                {
                    richTextBoxWithLinking.SelectionStart = richTextBoxWithLinking.Text.Length;
                    if (logObject.Link != "")
                    {
                        var textParts = Regex.Split(logObject.Text, "{LINK}");
                        richTextBoxWithLinking.SelectedText = textParts[0];
                        richTextBoxWithLinking.InsertLink(logObject.LinkText, richTextBoxWithLinking.Text.Length);
                        if (!linkDictionary.ContainsKey(logObject.LinkText)) linkDictionary.Add(logObject.LinkText, logObject.Link);
                        richTextBoxWithLinking.SelectionStart = richTextBoxWithLinking.Text.Length;
                        richTextBoxWithLinking.SelectedText = textParts[1] + Environment.NewLine;

                        Store += logObject.Text.Replace("{LINK}", "<a href=\"" + logObject.Link + "\">" + logObject.LinkText + "</a>") + Environment.NewLine;
                    }
                    else
                    {
                        richTextBoxWithLinking.SelectedText = logObject.Text + Environment.NewLine;
                        Store += logObject.Text + Environment.NewLine;
                    }
                }

                int maxLines = 200;

                if (richTextBoxWithLinking.Lines.Length > maxLines)
                {
                    string[] newArray = new string[maxLines];
                    Array.Copy(richTextBoxWithLinking.Lines, richTextBoxWithLinking.Lines.Length - maxLines, newArray, 0, maxLines);
                    richTextBoxWithLinking.Text = string.Join(Environment.NewLine, newArray);
                }

                try
                {
                    richTextBoxWithLinking.SelectionStart = selectionStart;
                    richTextBoxWithLinking.SelectionLength = selectionLength;
                }
                catch { }
                SendMessage(richTextBoxWithLinking.Handle, WM_VSCROLL, (IntPtr)SB_BOTTOM, IntPtr.Zero);
            }
        }

        public class LogOject
        {
            public string Text { get; set; }
            public string LinkText { get; set; }
            public string Link { get; set; }
            public override string ToString()
            {
                return Text + Link;
            }
        }

        private void richTextBoxWithLinking_LinkClicked(object sender, LinkClickedEventArgs e)
        {
            Process.Start(linkDictionary[e.LinkText]);
        }

        Dictionary<string, string> linkDictionary = new Dictionary<string, string>();
        public string Store = "";


        public void PerformLog(string text, string link = "", string linkText = "", bool EmptyStore = false)
        {
            var logObject = new Log.LogOject { Text = DateTime.Now.ToLongTimeString() + " - " + text, Link = link, LinkText = linkText };
            logObjects.Add(logObject);
            if (EmptyStore) Store = "";
        }
    }
}
